
var regWord = /^(\w+)$/;

/**
 * 用户名输入框获取焦点，设置输入框校验显示信息列的样式
 * 
 * @param td_id_accountCol 用户账号输入校验显示列
 * @param s_id_accountDesc 提示信息显示Span ID
 */
function accountFocus(td_id_accountCol, s_id_accountDesc) {
    var td_id_accountColObj = document.getElementById(td_id_accountCol);
    var s_id_accountDescObj = document.getElementById(s_id_accountDesc);
    var infoMsg = "后台登录账号，必填，字母、数字、下划线，最长16个字符";
    displayInfoStyle(td_id_accountColObj, s_id_accountDescObj, infoMsg);
}

/**
 * 用户名输入框失去焦点时的校验
 * 
 * @param i_account_obj 用户名输入信息框焦点
 * @param td_id_accountCol
 * @param s_id_accountDesc
 */
function accountBlur(i_account_obj, td_id_accountCol, s_id_accountDesc) {
	var chkResult = true;
	
    var accountV = i_account_obj.value;
    if(accountV) {
        accountV = accountV.trim();
        i_account_obj.value = accountV;
    }
    
    var td_id_accountColObj = document.getElementById(td_id_accountCol);
    var s_id_accountDescObj = document.getElementById(s_id_accountDesc);
    
    var errMsg = "";
    if(accountV == "") {
        errMsg = "后台登录账号不能为空";
    } else if(!regWord.test(accountV)) {
        errMsg = "后台登录账号只能为数字、字母、下划线";
    } else if(accountV.length > 16 ) {
        errMsg = "后台登录账号最大长度为16个字符";
    }
    
    if(errMsg != "") {
        displayErrorStyle(td_id_accountColObj, s_id_accountDescObj, errMsg);
        chkResult = false;
        return chkResult;
    }
    
    // 校验通过
    displayOkStyle(td_id_accountColObj, s_id_accountDescObj);
    
    return chkResult;
}

/**
 * 密码输入框获取焦点，显示提示信息
 * @param td_id_pwdCol 密码输入信息显示TD列
 * @param s_id_pwdDesc 密码输入信息显示Span
 */
function passwordFocus(td_id_pwdCol, s_id_pwdDesc) {
	var td_id_pwdColObj = document.getElementById(td_id_pwdCol);
    var s_id_pwdDescObj = document.getElementById(s_id_pwdDesc);
    var infoMsg = "后台登录密码，必填，字母、数字、下划线等字符";
    displayInfoStyle(td_id_pwdColObj, s_id_pwdDescObj, infoMsg);
}

/**
 * 密码输入框失去焦点，进行密码的校验
 * @param i_password_obj 密码输入框
 * @param td_id_pwdCol
 * @param s_id_pwdDesc
 */
function passwordBlur(i_password_obj, td_id_pwdCol, s_id_pwdDesc) {
	var chkResult = true;
	
	var pwdV = i_password_obj.value;
    var td_id_pwdColObj = document.getElementById(td_id_pwdCol);
    var s_id_pwdDescObj = document.getElementById(s_id_pwdDesc);
    
    var errMsg = "";
    if(pwdV == "") {
        errMsg = "后台登录密码不能为空";
    } else if(pwdV.length < 6 || pwdV.length > 16) {
    	errMsg = "后台登录密码长度为6~16个字符";
    }
    
    if(errMsg != "") {
        displayErrorStyle(td_id_pwdColObj, s_id_pwdDescObj, errMsg);
        chkResult = false;
        return chkResult;
    }
    
    // 校验通过
    displayOkStyle(td_id_pwdColObj, s_id_pwdDescObj);
    
    return chkResult;
}

/**
 * 密码确认输入框失去焦点，进行校验
 * @param i_pwd2_obj 密码确认输入框
 * @param i_id_pwd 密码输入框ID
 * @param td_id_pwd2Col 密码确认信息显示TD
 * @param s_id_pwd2Desc 密码确认信息显示Span
 */
function pwd2Blur(i_pwd2_obj, i_id_pwd, td_id_pwd2Col, s_id_pwd2Desc) {
	var chkResult = true;
	
	var pwd2V = i_pwd2_obj.value;
	
	var i_id_pwdObj = document.getElementById(i_id_pwd);
	var td_id_pwd2ColObj = document.getElementById(td_id_pwd2Col);
    var s_id_pwd2DescObj = document.getElementById(s_id_pwd2Desc);
    
    var errMsg = "";
    if(pwd2V == "") {
        errMsg = "后台登录确认密码不能为空";
    } else if(pwd2V.length < 6 || pwd2V.length > 16) {
    	errMsg = "后台登录密码长度为6~16个字符";
    } else {
    	var pwdV = i_id_pwdObj.value;
    	if(pwdV != pwd2V) {
    		errMsg = "后台登录密码两次输入不一致，请重新输入";
    	}
    }
    
    if(errMsg != "") {
        displayErrorStyle(td_id_pwd2ColObj, s_id_pwd2DescObj, errMsg);
        chkResult = false;
        return chkResult;
    }
    
    // 校验通过
    displayOkStyle(td_id_pwd2ColObj, s_id_pwd2DescObj);
    
    return chkResult;
}

/**
 * 添加新管理员用户表单提交
 */
function adminsCfgFormFun() {
	// 校验用户名
	var accountObj = document.getElementById("account");
	if(!accountBlur(accountObj, 'accountCol', 'accountDesc')) {
		//accountObj.focus();
		return false;
	}
	
	// 校验密码
	var pwdObj = document.getElementById("password");
	if(!passwordBlur(pwdObj, 'pwdCol', 'pwdDesc')) {
		//pwdObj.focus();
		return false;
	}
	
	// 校验确认密码
	var pwd2Obj = document.getElementById("pwd2");
	if(!pwd2Blur(pwd2Obj, 'password', 'pwdCol2', 'pwdDesc2')) {
		//pwd2Obj.focus();
		return false;
	}
	
	document.getElementById("adminsCfgForm").submit();
    return true;
}

/**
 * 编辑时提交表单验证
 * @returns {Boolean}
 */
function adminsCfgEditFormFun() {
	// 校验密码
	var pwdObj = document.getElementById("password");
	if(!passwordBlur(pwdObj, 'pwdCol', 'pwdDesc')) {
		//pwdObj.focus();
		return false;
	}
	
	// 校验确认密码
	var pwd2Obj = document.getElementById("pwd2");
	if(!pwd2Blur(pwd2Obj, 'password', 'pwdCol2', 'pwdDesc2')) {
		//pwd2Obj.focus();
		return false;
	}
	
	document.getElementById("adminsCfgForm").submit();
    return true;
}
